package com.pxmeta.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.pxmeta.common.constant.HttpStatus;
import com.pxmeta.common.exception.ServiceException;
import lombok.SneakyThrows;
import okhttp3.*;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;

public class AIGCUtil {

    public static final String API_KEY = "QG7i0np8DBRXCQ471zXCmr3G";
    public static final String SECRET_KEY = "LW1hXCvUQGzlZYGGP6dvFfBqAYxSNoYF";


    public static void main(String[] args) {
//        String taskId = draw("微笑，写真，高清摄影，全身，皮肤白皙，美女大学生，白色纱裙，瓜子脸，大眼睛，复杂的细节，绝美，完成度高，高清，高品质，4k", "写实风格");
//        System.out.println(taskId);
//////        System.out.println(taskId);
        String img = getImg("16163877");
        System.out.println(img);
    }

    @SneakyThrows
    public static String draw(String text, String style) {
//        MediaType mediaType = MediaType.parse("application/json");
//
//
//
//        com.alibaba.fastjson.JSONObject bodyContent = new com.alibaba.fastjson.JSONObject();
//        bodyContent.put("text", text);
//        bodyContent.put("style", style);
//        bodyContent.put("resolution","1024*1536");
//        bodyContent.put("num",1);
//
//
//
//        RequestBody body = RequestBody.create(mediaType, bodyContent.toJSONString());
//        Request request = new Request.Builder()
//                .url("https://aip.baidubce.com/rpc/2.0/ernievilg/v1/txt2img?access_token=" + getAccessToken())
//                .method("POST", body)
//                .addHeader("Content-Type", "application/json")
//                .addHeader("Accept", "application/json")
//                .build();
//        Response response = HTTP_CLIENT.newCall(request).execute();
//        com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(response.body().string());
//        Integer errorCode = jsonObject.getInteger("error_code");
//        if (errorCode != null){
//            throw new ServiceException(jsonObject.getString("error_msg"));
//        }
//        return jsonObject.getJSONObject("data").getString("taskId");

        return null;
    }


    @SneakyThrows
    public static String  getImg(String taskId){
//        MediaType mediaType = MediaType.parse("application/json");
//        RequestBody body = RequestBody.create(mediaType,  "{\"taskId\":\""+taskId+"\"}");
//        Request request = new Request.Builder()
//                .url("https://aip.baidubce.com/rpc/2.0/ernievilg/v1/getImg?access_token=" + getAccessToken())
//                .method("POST", body)
//                .addHeader("Content-Type", "application/json")
//                .addHeader("Accept", "application/json")
//                .build();
//        Response response = HTTP_CLIENT.newCall(request).execute();
//        com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(response.body().string());
//        if (jsonObject.getInteger("error_code") != null){
//            throw new ServiceException("参数错误！", HttpStatus.PARAMS_ERROR);
//        }
//        com.alibaba.fastjson.JSONObject data = jsonObject.getJSONObject("data");
//        Integer status = data.getInteger("status");
//
//        if (status == 0){
//            throw new ServiceException("绘画进行中！", HttpStatus.PENDING);
//        }
//
//        if (status == -1){
//            throw new ServiceException("参数错误！", HttpStatus.PARAMS_ERROR);
//        }
//
//        String imgUrl = data.getString("img");
//
//        return imgUrl;
        return null;

    }


    /**
     * 从用户的AK，SK生成鉴权签名（Access Token）
     *
     * @return 鉴权签名（Access Token）
     * @throws IOException IO异常
     */
    static String getAccessToken() throws IOException, JSONException {
//        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
//        RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY
//                + "&client_secret=" + SECRET_KEY);
//        Request request = new Request.Builder()
//                .url("https://aip.baidubce.com/oauth/2.0/token")
//                .method("POST", body)
//                .addHeader("Content-Type", "application/x-www-form-urlencoded")
//                .build();
//        Response response = HTTP_CLIENT.newCall(request).execute();
//        return new JSONObject(response.body().string()).getString("access_token");
        return null;
    }
}
